<template>
  <p>
    <a-space>
      <a-button type="primary" @click="handleQuery()">刷新</a-button>
      <a-button type="primary" @click="onAdd">新增</a-button>
    </a-space>
  </p>
  <a-table :dataSource="stations"
           :columns="columns"
           :pagination="pagination"
           @change="handleTableChange"
           :loading="loading">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'operation'">
        <a-space>
          <a-popconfirm
              title="删除后不可恢复，确认删除?"
              @confirm="onDelete(record)"
              ok-text="确认" cancel-text="取消">
            <a style="color: red">删除</a>
          </a-popconfirm>
          <a @click="onEdit(record)">编辑</a>
        </a-space>
      </template>
    </template>
  </a-table>
  <a-modal v-model:open="visible" title="车站" @ok="handleOk"
           ok-text="确认" cancel-text="取消">
    <a-form :model="station" :label-col="{span: 6}" :wrapper-col="{ span: 18 }">
      <a-form-item label="站名">
        <a-input v-model:value="station.name"/>
      </a-form-item>
      <a-form-item label="站名拼音">
        <a-input v-model:value="station.namePinyin" :disabled="true"/>
      </a-form-item>
      <a-form-item label="站名拼音首字母">
        <a-input v-model:value="station.namePy" :disabled="true"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts" name="station-view">
import {ref, onMounted, watch} from 'vue';
import {notification} from "ant-design-vue";
import {save, query, deleteById} from "@/api/base/station";
import {QueryParams, SaveParams} from "@/api/base/station/type";
import {copyObj, isNotEmpty, capitalizeFirstLetter} from "@/utils/tools";
import {pinyin} from "pinyin-pro";

const visible = ref(false);
let station = ref({
  id: "",
  name: "",
  namePinyin: "",
  namePy: "",
  createTime: "",
  updateTime: "",
});
const stations = ref([]);

watch(() => station.value.name, (value: string) => {
  if (isNotEmpty(value)) {
    station.value.namePinyin = capitalizeFirstLetter(pinyin(value, {toneType: 'none', separator: ""}))
    station.value.namePy = pinyin(value, {toneType: 'none', pattern: "first", separator: ""})
  } else {
    station.value.namePinyin = "";
    station.value.namePy = "";
  }
})
// 分页的三个属性名是固定的
const pagination = ref({
  total: 0,
  current: 1,
  pageSize: 15,
});
let loading = ref(false);
const columns = [
  {
    title: '站名',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '站名拼音',
    dataIndex: 'namePinyin',
    key: 'namePinyin',
  },
  {
    title: '站名拼音首字母',
    dataIndex: 'namePy',
    key: 'namePy',
  },
  {
    title: '操作',
    dataIndex: 'operation'
  }
];

const onAdd = () => {
  station.value = {};
  visible.value = true;
};

const onEdit = (record: SaveParams) => {
  station.value = copyObj(record);
  visible.value = true;
};

const onDelete = async (record: SaveParams) => {
  try {
    const result = await deleteById(record.id as number);
    if (result.success) {
      notification.success({message: "操作成功！", description: "删除成功！"});
      await handleQuery({
        pageNum: pagination.value.current,
        pageSize: pagination.value.pageSize,
      });
    } else {
      notification.error({message: undefined, description: result.message});
    }
  } catch (e: any) {
    notification.error({message: undefined, description: e.message});
  }
};

const handleOk = async () => {
  try {
    const result = await save(station.value);
    if (result.success) {
      notification.success({message: "操作成功", description: "保存成功！"});
      visible.value = false;
      await handleQuery({
        pageNum: pagination.value.current,
        pageSize: pagination.value.pageSize
      });
    } else {
      notification.error({message: "操作失败", description: result.message});
    }
  } catch (e: any) {
    notification.error({message: "操作失败", description: e.message});
  }
};

const handleQuery = async (param: QueryParams) => {
  if (!param) {
    param = {
      pageNum: 1,
      pageSize: pagination.value.pageSize
    };
  }
  loading.value = true;
  try {
    const result = await query(param);
    loading.value = false;
    if (result.success) {
      stations.value = result.data as any;
      // 设置分页控件的值
      pagination.value.current = param.pageNum as number;
      pagination.value.total = result.total;
    } else {
      notification.error({message: "失败", description: result.message});
    }
  } catch (e: any) {
    notification.error({message: "失败", description: e.message});
  }
};

const handleTableChange = (page: any) => {
  // console.log("看看自带的分页参数都有啥：" + JSON.stringify(page));
  pagination.value.pageSize = page.pageSize;
  handleQuery({
    pageNum: page.current,
    pageSize: page.pageSize
  });
};

onMounted(() => {
  console.log("station-view onMounted")
  handleQuery({
    pageNum: 1,
    pageSize: pagination.value.pageSize
  });
})
</script>
